package basic.nio;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;

import java.nio.channels.SocketChannel;
import java.util.Date;

import java.util.Scanner;

public class SocketNoneBlockNIOClient {


    public void  client(){

        try {
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("localhost",30401));
            socketChannel.configureBlocking(false);
            ByteBuffer byteBuffer  = ByteBuffer.allocate(1024);
            Scanner scanner = new Scanner(System.in);
            //System.out.println("阻塞~~~~~~~~~~~~");
           // BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while (scanner.hasNext()){
                 String msg = scanner.next();
                byteBuffer.put((new Date().toString()+"："+msg).getBytes());
                byteBuffer.flip();
                socketChannel.write(byteBuffer);
                byteBuffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new SocketNoneBlockNIOClient().client();
    }


}
